package com.taotao.sso.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.CookieUtils;
import com.taotao.common.utils.ExceptionUtil;
import com.taotao.pojo.TbUser;
import com.taotao.sso.service.IUserService;

/**
 * 用户信息
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private IUserService userService;

	@RequestMapping("/check/{param}/{type}")
	@ResponseBody
	public Object chechDate(@PathVariable String param,
			@PathVariable Integer type, String callback) {
		TaotaoResult taotaoResult = null;
		if (StringUtils.isBlank(param)) {
			taotaoResult = TaotaoResult.build(400, "传递的参数不能为空");
		}

		if (null == type) {
			taotaoResult = TaotaoResult.build(400, "传递的类型参数不能为空");
		}

		if (1 != type && 2 != type && 3 != type) {
			taotaoResult = TaotaoResult.build(400, "传递的参数类型不正确");
		}
		if (null == taotaoResult) {
			try {
				taotaoResult = this.userService.checkDate(param, type);
			} catch (Exception e) {
				e.printStackTrace();
				return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
			}
		}

		if (StringUtils.isNotBlank(callback)) {
			MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(
					taotaoResult);
			mappingJacksonValue.setJsonpFunction(callback);
			return mappingJacksonValue;
		} else {
			return taotaoResult;
		}
	}

	/**
	 * 添加用户
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	@ResponseBody
	public TaotaoResult AddUser(TbUser user) {
		try {
			TaotaoResult taotaoResult = this.userService.addUser(user);
			return taotaoResult;
		} catch (Exception e) {
			e.printStackTrace();
			return TaotaoResult.build(400, "注册失败. 请校验数据后请再提交数据.",
					ExceptionUtil.getStackTrace(e));
		}
	}

	/**
	 * 用户登录
	 * 
	 * @param username
	 * @param password
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public TaotaoResult userLogin(String username, String password,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			TaotaoResult taotaoResult = this.userService.userLogin(username,
					password);
			// 添加cookie 信息
			String token = (String) taotaoResult.getData();
			CookieUtils.setCookie(request, response, "TT_TOKEN", token);
			return taotaoResult;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
		}

	}

	/**
	 * 通过token查询用户信息
	 * 
	 * @param token
	 * @return
	 */
	@RequestMapping(value = "/token/{token}", method = RequestMethod.GET)
	@ResponseBody
	public Object getUserByToken(@PathVariable String token, String callback) {
		TaotaoResult taotaoResult = null;
		try {
			taotaoResult = this.userService.getUserByToken(token);
			if (StringUtils.isNotBlank(callback)) {
				MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(
						taotaoResult);
				mappingJacksonValue.setJsonpFunction(callback);
				return mappingJacksonValue;
			}
			return taotaoResult;
		} catch (Exception e) {
			e.printStackTrace();
			return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
		}

	}

	/**
	 * 用户退出
	 * 
	 * @param token
	 * @return
	 */
	@RequestMapping("/logout/{token}")
	@ResponseBody
	public TaotaoResult logoutByToken(@PathVariable String token) {
		try {
			TaotaoResult taotaoResult = this.userService.logoutByToken(token);
			return taotaoResult;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
		}
	}
}
